package tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

import model.Extensions;

import org.ini4j.Ini;
import org.ini4j.InvalidFileFormatException;
import org.ini4j.Profile.Section;

import util.AuxExtensions;
import util.AuxExtensionsGlobals;

public class Teste {

	public static ArrayList<Extensions> listExtensions() {
    	
    	ArrayList<Extensions> lista = new ArrayList<Extensions>(0);
    	
		try {

			File f = new File("/etc/asterisk/extensions.conf");

			BufferedReader in = new BufferedReader(new FileReader(f));

			Ini conf = new Ini();
			conf.load(in);
			
			Class<?> cls = Extensions.class;

			Extensions extensions = new Extensions(); //Model.getModelInstance(cls);
			
			Field[] fields = cls.getDeclaredFields();

			Field field = null;
			
			String fieldName = null;
			
			Object fieldValue = null;
			
			String methodName = null;

			Method method = null;
			
			Section section = null;
			
			AuxExtensions aux = null;
			
			AuxExtensionsGlobals auxGlobals = null;
			
			String[] array = null;
			
			String auxAplicacao = null;
			
			// Varrendo todas as seções do arquivo
			for (String sectionName: conf.keySet()) {
				
				section = conf.get(sectionName);
				
				extensions = new Extensions();
				
				extensions.setId(sectionName);
				
				if (section.getName().matches("general")) {
					
					for (int i = 0; i < fields.length; i++) {
						
						field = fields[i];

						fieldName = field.getName();
						
						fieldValue = section.get(fieldName.toLowerCase().replace("_", ""));
						
						if (fieldValue != null) {
						
							methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
			
							method = cls.getMethod(methodName, field.getType());
							
							method.invoke(extensions, fieldValue);
						}
					}
					
				} else if (section.getName().matches("globals")) {
					
					for (String element: section.keySet()) {
					
						auxGlobals = new AuxExtensionsGlobals();
						
						auxGlobals.setChave(element);
						auxGlobals.setValor(section.get(element));
						
						extensions.getGlobals().add(auxGlobals);
					}
					
				} else {
					
					for (String element: section.getAll("exten")) {
						
						aux = new AuxExtensions();
						
						auxAplicacao = "";
						
						array = element.split(",");
						
						aux.setNumExtensao(array[0].replace(">", "").trim());
						aux.setPrioridade(Integer.parseInt(array[1]));
						
						// O 3o parâmetro pode conter vírgulas, então ele será quebrado em posições do array.
						// Para resolver isto, a partir da 3a posição fazemos um "for" para pegar todas as
						// posições seguintes.
						auxAplicacao = array[2];
						
						for (int i = 3; i < array.length; i++) {
							auxAplicacao += "," + array[i];
						}
						aux.setAplicacao(auxAplicacao);
						
						extensions.getLinhas().add(aux);
					}
				}
				
				lista.add(extensions);
	    	}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidFileFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return lista;
    }
	
	public static void main(String[] args) {
		
		ArrayList<Extensions> lista = listExtensions();
		
		System.out.println("debugger");
		
		/*try {
			File f = new File("/etc/asterisk/extensions.conf");

			BufferedReader in = new BufferedReader(new FileReader(f));
			
			Ini arquivo = new Ini();
			arquivo.load(in);
			
			Section secao = arquivo.get("LOCAL");
			
			List<String> lista = secao.getAll("exten");
			
			System.out.println(lista);
			
			//System.out.println(general.get("context"));
			
			
			

			// arquivo.load(input)
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		/*Properties props = new Properties();
        try {
			props.load(new FileInputStream("paths.properties"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/*try {
			
			File f = new File("/etc/asterisk/sip.conf");
			
			BufferedReader in;
			
			in = new BufferedReader(new FileReader(f));
			
			Ini conf = new Ini();
			conf.load(in);
			
			conf.add("6555");
			
			System.out.println(conf);
			
			//conf.store();
			Section sec = conf.get("6555");
			
			System.out.println(conf);
			
			sec.add("callerId", 20);
			
			System.out.println(conf);
			
			File f2 = new File("/etc/asterisk/sip_temp.conf");
			
			conf.store(f2);
			
			System.out.println(conf);
			
			f.delete();
			
			f2.renameTo(f);
			
			//Section sec = conf.get("teste");
			
			//sec.add("callerid", "11");
			
			//conf.setFile(f);
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidFileFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		System.out.println(("_").toLowerCase());
		
	}

}
